package uiController;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import adapter.ConfigFileAdapter;
import data.NameConstants;
import data.SiloAdress;

public class UIStartController implements Initializable {
	public static final String UIConten_FXML_FILE = "/dialog/UIContent.fxml";
	public static final String ErrorDialog_FXML_FILE = "/dialog/ErrorDialog.fxml";

	@FXML
	private TextField txtPort;

	@FXML
	private Button btnCancel;

	@FXML
	private Button btnConnect;

	@FXML
	private TextField txtIPAdress;

	@FXML
	private Button btnLoadConfig;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {

	}

	@FXML
	void btnConnectClick(ActionEvent event) throws IOException {
		ArrayList<SiloAdress> tmp = new ArrayList<SiloAdress>();
		// if (!this.txtIPAdress.getText().isEmpty()
		// && !this.txtPort.getText().isEmpty()
		// && !this.txtIPAdress.getText().equalsIgnoreCase("IP Adress")
		// && !this.txtPort.getText().equalsIgnoreCase("Port")) {
		//
		// SiloAdress siloAdress = new SiloAdress(this.txtIPAdress.getText(),
		// this.txtPort.getText());
		// tmp.add(siloAdress);
		startUIContent(tmp);
		((Stage) this.btnCancel.getScene().getWindow()).close();
		// }
	}

	@FXML
	void btnCancelClick(ActionEvent event) {
		((Stage) this.btnCancel.getScene().getWindow()).close();

	}

	@FXML
	void btnLoadConfigClick(ActionEvent event) throws IOException {
		ArrayList<SiloAdress> siloAdress = null;
		Stage stage = new Stage();
		FileChooser fileChooser = new FileChooser();
		fileChooser.getExtensionFilters().add(
				new FileChooser.ExtensionFilter("XML files (*.xml)", "*.xml"));
		fileChooser.setTitle(NameConstants.FILECHOSSER_TITEL);
		File file = fileChooser.showOpenDialog(stage);
		if (file != null) {
			try {
				siloAdress = new ConfigFileAdapter().readConfigFile(file);
				startUIContent(siloAdress);
				((Stage) this.btnCancel.getScene().getWindow()).close();
			} catch (Exception e) {
				FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
						ErrorDialog_FXML_FILE));

				Parent root = (Parent) fxmlLoader.load(getClass().getResource(
						ErrorDialog_FXML_FILE).openStream());

				Stage alterStage = new Stage();
				alterStage.setTitle(NameConstants.DIALOG_TITEL);
				alterStage.setResizable(false);
				alterStage.setScene(new Scene(root, 300, 180));
				alterStage.show();

			}

		}

	}

	private void startUIContent(ArrayList<SiloAdress> adress)
			throws IOException {
		FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
				UIConten_FXML_FILE));

		Parent root = (Parent) fxmlLoader.load(getClass().getResource(
				UIConten_FXML_FILE).openStream());

		final UIContentController uiContentController = fxmlLoader
				.<UIContentController> getController();
		uiContentController.start(adress);
		Stage stage = new Stage();
		stage.setTitle(NameConstants.DIALOG_TITEL);
		stage.setResizable(false);
		stage.setScene(new Scene(root, 800, 510));
		stage.show();

	}

}
